package com.technology.jdbc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Properties;

import oracle.sql.NUMBER;

public class MetaDataUsing {

	public static void main(String[] args) {
		Connection oracleConnection = connect2Oracle();
		
		try {
			Statement stat = oracleConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			ResultSet rs = stat.executeQuery("select * from emp");
				
			ResultSetMetaData rsmd = rs.getMetaData();
			for (int i = 1; i <= rsmd.getColumnCount(); i++){
				String columnName = rsmd.getColumnName(i);
				Object value;
				rs.beforeFirst(); 
				while (rs.next()) {
					switch(rsmd.getColumnType(i)){
						case Types.NUMERIC : value = rs.getBigDecimal(columnName); break;
						case Types.DATE : value = rs.getDate(columnName); break;
						case Types.VARCHAR :
						default :  value = rs.getString(columnName);
					}
					System.out.println("Column : " + columnName + " value " + value);
				}
			
//				System.out.println("Column : " + columnName);
			}
			
			oracleConnection.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public static void getDatabaseInfo() throws SQLException{
		Connection conn = connect2Oracle();
		DatabaseMetaData dbmd = conn.getMetaData();
	}
	
	public static Connection connect2Oracle(){
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Properties prop = new Properties();
			prop.load(new FileInputStream("src/com/technology/jdbc/db.properties"));
			return DriverManager.getConnection(prop.getProperty("db.url"), prop.getProperty("db.user"), prop.getProperty("db.password"));
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
